#include "stdafx.h"
#include "Helper.h"

struct Vertex		///  Mesh vertex struct.
{
	float x;		///< Position x of vertex in 3D space
	float y;		///< Position y of vertex in 3D space
	float z;		///< Position z of vertex in 3D space
	DWORD diffuse;	///< Diffuse color of vertex
};

void DrawMeshColor(LPDIRECT3DDEVICE9 pd3dDevice,LPD3DXMESH *ppMesh, float fR, float fG, float fB, float fA)
{
	if (ppMesh == NULL) return;

	LPDIRECT3DVERTEXBUFFER9 pTempVertexBuffer;
	LPD3DXMESH pOldMesh = *ppMesh;

	pOldMesh->CloneMeshFVF(0, D3DFVF_XYZ | D3DFVF_DIFFUSE, pd3dDevice, ppMesh);

	if(SUCCEEDED((*ppMesh)->GetVertexBuffer(&pTempVertexBuffer)))
	{
		int nNumVerts = (*ppMesh)->GetNumVertices();
		Vertex *pVertices = NULL;

		pTempVertexBuffer->Lock(0, 0, (void**)&pVertices, 0);
		{
			for(int i = 0; i < nNumVerts; ++i)
			{
				pVertices[i].diffuse = D3DCOLOR_COLORVALUE(fR, fG, fB, fA);
			}
		}
		pTempVertexBuffer->Unlock();

		pTempVertexBuffer->Release();
	}

	pOldMesh->Release();
}